package com.bitcat.todoapp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.animation.BounceInterpolator
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bitcat.todoapp.R
import com.bitcat.todoapp.databinding.ActivitySplashBinding
import kotlin.concurrent.thread

class SplashActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySplashBinding

    private lateinit var countDownTimer: CountDownTimer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

//        val customFont = Typeface.createFromAsset(assets, "fonts/a1.ttf")
//        binding.titleTv.typeface = customFont

        val customFont = ResourcesCompat.getFont(this, R.font.a1)
        binding.titleTv.typeface = customFont


        startAnimation()
        countDown()


        //主线程（UI线程）
        Log.d("打印日志", Thread.currentThread().name)

        thread {
            //子线程(工作线程)
            Log.d("打印日志", Thread.currentThread().name)
        }
        thread {
            //子线程(工作线程)
            Log.d("打印日志", Thread.currentThread().name)
        }
    }

    private fun countDown() {
        countDownTimer = object : CountDownTimer(5000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                binding.timeTv.text = (millisUntilFinished / 1000 + 1).toString()
                //实现一个缩放动画，先150ms放大1.5倍，150ms缩小到原始大小
                binding.timeTv.animate()
                    .scaleX(1.5f)
                    .scaleY(1.5f)
                    .setDuration(150)
                    .withEndAction {
                        binding.timeTv.animate()
                            .scaleX(1f)
                            .scaleY(1f)
                            .setDuration(150)
                            .start()
                    }
                    .start()
            }

            override fun onFinish() {

                val sp = getSharedPreferences("todo", MODE_PRIVATE)
                val isLogin = sp.getBoolean("is_login", false)
                if (isLogin) {
                    // 跳转首页
                    val intent = Intent(this@SplashActivity, MainActivity::class.java)
                    startActivity(intent)
                } else {
                    // 跳转登录页
                    val intent = Intent(this@SplashActivity, LoginActivity::class.java)
                    startActivity(intent)
                }
                finish()  //关闭启动页

            }
        }.start()
    }

    private fun startAnimation() {
        binding.titleTv.apply {
            alpha = 0f
            translationY = 500f
            scaleX = 0.8f
            scaleY = 0.8f
            rotation = 90f
            animate()
                .alpha(1f)
                .translationY(0f)
                .rotation(0f)
                .setDuration(1500)
                .withEndAction {
                    animate()
                        .scaleX(1.2f)
                        .scaleY(1.2f)
                        .setDuration(800)
                        .setInterpolator(BounceInterpolator())
                        .start()
                }
                .start()
        }

//        binding.titleTv.alpha = 0f
//        binding.titleTv.translationY = 500f
//        binding.titleTv.scaleX = 0.8f
//        binding.titleTv.scaleY = 0.8f
    }
}